<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020/5/9
 * Time: 14:51
 */

namespace App\Rpc\Traits\Service;

use Dotenv\Exception\ValidationException;
use Swoft\Bean\BeanFactory;
use App\Lib\Error\Response;
use App\Rpc\Model\Dao\PermissionsDao;
use App\Rpc\Model\Logic\PermissionLogic;

trait PermissionTrait
{
    /**
     * @param array $data
     * @return array
     */
    public function permissionCreate(array $data): array
    {
        //验证数据是否合理
        $diyValidator = [
            'PermissionValidator' => [ 'op' => 'add']
        ];
        try{
            //添加item_id
            $data['item_id'] = create_item_id(uuid());
            //这里首先验证中间件
            validate($data, "PermissionValidator", [], $diyValidator);
            /** @var  PermissionsDao $Dao */
            $Dao = BeanFactory::getBean(PermissionsDao::class);
            $result = $Dao->insert($data);
            return Response::Info(['item_id' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
        //参数：$data['item_id'] ...
        //新增插入
    }

    /**
     * @param array $data
     * @return array
     */
    public function permissionUpdate(array $data): array
    {
        //验证数据是否合理更新
        $diyValidator = [
            'PermissionValidator' => [ 'op' => 'update']
        ];
        try{
            //添加item_id
            $id = $data['item_id']??null;
            unset($data['item_id']);

            //这里首先验证中间件
            validate($data, "PermissionValidator", [], $diyValidator);
            /** @var PermissionsDao $Dao */
            $Dao = BeanFactory::getBean(PermissionsDao::class);
            $result = $Dao->update($id, $data);
            return Response::Info(['state' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
        //参数：$data['permission_item_id']
        //参数：$data['update_data'] = []
        //修改某个权限
    }

    /**
     * @param array $data
     * @return array
     */
    public function permissionDelete(array $data): array
    {
        try{
            //添加item_id
            $id = $data['item_id']??null;
            unset($data['item_id']);

            /** @var PermissionsDao $Dao */
            $Dao = BeanFactory::getBean(PermissionsDao::class);
            $result = $Dao->delete($id);

            return Response::Info(['state' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
        //参数：$data['permission_item_id']
        //删除某个权限
    }

    /**
     * @param array $data
     * @return array
     */
    public function listSubLevelPermissions(array $data): array
    {
        try{
            //参数验证
            $permission_value = $data['permission_value']??null;
            $user_item_id = $data['user_item_id']??null;

            if(!$permission_value || !$user_item_id){
                return Response::FatalError(4002, '请求参数错误');
            }

            //接下来查询所有得role_permissions
            /** @var PermissionLogic $PermissionLogic */
            $PermissionLogic = BeanFactory::getBean(PermissionLogic::class);
            $result = $PermissionLogic->listSubLevelPermissions($data);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
        //用户
        //节点
        //参数$data['user_item_id']
        //参数$data['item_id']
        //向下取得所有
    }

    /**
     * 验证密码
     * @param array $data
     * @return array
     */
    public function checkPassword(array $data): array
    {
        try{
            if(!isset($data['password']) || !$data['password']){
                throw new ValidationException('请输入密码');
            }
            if(!isset($data['account']) || !$data['account']){
                throw new ValidationException('请输入账号');
            }

            //接下来查询所有得role_permissions
            /** @var PermissionLogic $PermissionLogic */
            $PermissionLogic = BeanFactory::getBean(PermissionLogic::class);
            return $PermissionLogic->checkPassword($data);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }
}
